# coding: UTF-8

class Cell(object):
    def __init__(self):
        self.mark = " "
    
    def set(self, mark):
        self.mark = mark
    
    def getmark(self):
        return self.mark
    
    def is_empty(self):
        return self.mark == " "

LINES = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6))

class Board(object):
    def __init__(self):
        self.cells = [Cell() for i in range(9)]
    
    def display(self):
        print("+---+---+---+")
        cell_line = "|"
        for i in range(9):
            cell_line += " " + self.cells[i].getmark() + " |"
            if i % 3 == 2:
                print(cell_line)
                cell_line = "|"
                print("+---+---+---+")
    
    def is_empty(self, index):
        return self.cells[index].is_empty()
    
    def __count(self):
        count = 0
        for current in self.cells:
            if not current.is_empty():
                count += 1
        return count
    
    def __mark(self, count):
        if count % 2:
            return "X"
        else:
            return "O"
    
    def winner(self):
        return self.__mark(self.__count() - 1)
    
    def put(self, index):
        self.cells[index].set(self.__mark(self.__count()))
    
    def is_lined(self):
        for current in LINES:
            if (self.cells[current[0]].getmark() == "O" and self.cells[current[1]].getmark() == "O" and self.cells[current[2]].getmark() == "O") or \
               (self.cells[current[0]].getmark() == "X" and self.cells[current[1]].getmark() == "X" and self.cells[current[2]].getmark() == "X"):
                return True
        return False
    
    def __is_filled(self):          # 意味を明確にするためにメソッドにした
        return self.__count() > 8
    
    def is_finished(self):
        return self.__is_filled() or self.is_lined()

if __name__ == '__main__':
    board = Board()                         # Board クラスを生成
    while not board.is_finished():          # Board クラス経由に変更
        row = input("Rows? : ")
        if row not in(1,2,3):
            print("Input 1, 2 or 3.")
            continue
        
        column = input("Column? : ")
        if column not in(1,2,3):
            print("Input 1, 2 or 3.")
            continue
        
        target_index = (row - 1) * 3 + (column - 1)
        if board.is_empty(target_index):    # Board クラス経由に変更
            board.put(target_index)         # Board クラス経由に変更
        else:
            print("Not empty. Try another.")
            continue
        board.display()
    
    if board.is_lined():
        print("Game over. " + board.winner() + " won.")
    else:
        print("Game over. Draw.")
